/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package planspiel.panels.subpanels;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import planspiel.GameRound;
import planspiel.panels.GameScreen;
import admintool.imp03_data.IMP03_City;
import admintool.imp03_data.IMP03_Skill;
import admintool.imp03_data.IMP03_Vacancy;

/**
 *
 * @author Administrator
 */
public class GameContentVacancy extends JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = -8201128979252860710L;
	private GameScreen gameScreen = null;
    private JPanel vacancyParameters = null;
    private JComboBox cbVacancy = null;
    private IMP03_Vacancy selectedVacancy = null;
    private Image image = null;

    
    private JComboBox skillsCombo = null;
    private JLabel skillLevel = null;
    
    /**
     * 
     * @param gameScreen
     */
    public GameContentVacancy(GameScreen gameScreen) {
        this.gameScreen = gameScreen;
        initDisplay(gameScreen);
        
        //Set Background Image
        try {
            BufferedImage tmpImage = new BufferedImage(1000, 800, BufferedImage.TYPE_INT_RGB);
            tmpImage = ImageIO.read(this.getClass().getResourceAsStream("/planspiel/resources/BackgroundVacancy.jpg"));
            this.image =  new ImageIcon(tmpImage.getScaledInstance(720, 580, Image.SCALE_SMOOTH)).getImage();
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }

    /**
     * 
     * @return
     */
    public IMP03_Vacancy getSelectedVacancy() {
        return selectedVacancy;
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
        //super.paintComponent(g);
    }
    
    private void initDisplay(final GameScreen gameScreen){
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        
        JLabel test = new JLabel();
        cbVacancy = null;
        if(gameScreen.getGame().getCurrentGameRoundController().getSelectedVacancy() != null){
            cbVacancy = new JComboBox();
            cbVacancy.addItem(gameScreen.getGame().getCurrentGameRoundController().getSelectedVacancy());
        } else {
            cbVacancy = new JComboBox(gameScreen.getGame().getValidVacanciesForThisRound());
        }

        
        Dimension cbVacancyDimension = new Dimension(250, test.getHeight());
        cbVacancy.setMaximumSize(cbVacancyDimension);
        //cbVacancy.setMinimumSize(cbVacancyDimension);
        cbVacancy.setPreferredSize(cbVacancyDimension);

        vacancyParameters = new JPanel();
        vacancyParameters.setOpaque(false);

        cbVacancy.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                selectedVacancy = (IMP03_Vacancy)(cbVacancy.getSelectedItem());
                GameRound gameRound = gameScreen.getGame().getGameRound();
                
                vacancyParameters.removeAll();
                GroupLayout layoutVacancy = new GroupLayout(vacancyParameters);
                layoutVacancy.setAutoCreateGaps(true);
                vacancyParameters.setLayout(layoutVacancy);

                //JComponent definitions
                JLabel label1 = new JLabel("Title: ");
                JLabel label2 = new JLabel("Desc: ");
                JLabel label25 = new JLabel("Company: ");
                JLabel label3 = new JLabel("PLZ: ");
                JLabel label4 = new JLabel("StartDate [YYYY-MM-DD]: ");
                JLabel label5 = new JLabel("Qualification: ");
                JLabel label6 = new JLabel("Experience [Years]: ");
                JLabel label7 = new JLabel("Budget [Euro]: ");
                JLabel label8 = new JLabel("Anual Salary [Euro]: ");
                JLabel label9 = new JLabel("FullTime: ");
                JLabel label10 = new JLabel("Skills: ");
                
                JLabel text1 = new JLabel(selectedVacancy.getVacancyTitle());
                JLabel text2 = new JLabel(selectedVacancy.getVacancyDesc());
                JLabel combo25 = new JLabel(selectedVacancy.getVacancyCompanyName());

                JLabel tmpCombo51 = new JLabel(selectedVacancy.getVacancyPLZ());
                JLabel tmpLabel52 = new JLabel(IMP03_City.getSpecificCitiesFromDB(selectedVacancy.getVacancyPLZ()).getCityName());

                JLabel date4 = new JLabel(String.valueOf(selectedVacancy.getVacancyStartDate()));
 
                JLabel combo5 = new JLabel(selectedVacancy.getVacancyQualification());

                JLabel text6 = new JLabel(String.valueOf(selectedVacancy.getVacancyExperienceInYears()));
                JLabel text7 = new JLabel(String.valueOf(selectedVacancy.getVacancyBudget()));
                JLabel text8 = new JLabel(String.valueOf(selectedVacancy.getVacancyAnualSalary()));
                JLabel check9 = new JLabel("Is fulltime : " + selectedVacancy.isVacancyFullTimePartTime());

                skillsCombo = new JComboBox(selectedVacancy.getVacancySkills().toArray());
                skillsCombo.setMaximumSize(new Dimension(100,label1.getHeight()));
                skillLevel = new JLabel();
                skillsCombo.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        skillLevel.setText("SkillLevel: " + ((IMP03_Skill)(skillsCombo.getSelectedItem())).getSkillLevelForVacancy(selectedVacancy.getVacancyID()));
                    }
                });
                if(skillsCombo.getItemCount() > 0){
                    skillsCombo.setSelectedIndex(0);
                }

                //define layout
                layoutVacancy.setHorizontalGroup(
                    layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layoutVacancy.createSequentialGroup()
                            .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(label1)
                                .addComponent(label2)
                                .addComponent(label25)
                                .addComponent(label3)
                                .addComponent(label4)
                                .addComponent(label5)
                                .addComponent(label6)
                                .addComponent(label7)
                                .addComponent(label8)
                                .addComponent(label9)
                                .addComponent(label10)
                        )
                            .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(text1)
                                .addComponent(text2)
                                .addComponent(combo25)
                                .addGroup(layoutVacancy.createSequentialGroup()
                                    .addComponent(tmpCombo51)
                                    .addComponent(tmpLabel52)
                                )
                                .addComponent(date4)
                                .addComponent(combo5)
                                .addComponent(text6)
                                .addComponent(text7)
                                .addComponent(text8)
                                .addComponent(check9)
                                .addGroup(layoutVacancy.createSequentialGroup()
                                    .addComponent(skillsCombo)
                                    .addComponent(skillLevel)
                                )
                            )
                        )
                );

                layoutVacancy.setVerticalGroup(
                    layoutVacancy.createSequentialGroup()
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label1)
                            .addComponent(text1)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label2)
                            .addComponent(text2)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label25)
                            .addComponent(combo25)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label3)
                            .addComponent(tmpCombo51)
                            .addComponent(tmpLabel52)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label4)
                            .addComponent(date4)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label5)
                            .addComponent(combo5)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label6)
                            .addComponent(text6)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label7)
                            .addComponent(text7)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label8)
                            .addComponent(text8)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label9)
                            .addComponent(check9)
                        )
                        .addGroup(layoutVacancy.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(label10)
                            .addComponent(skillsCombo)
                            .addComponent(skillLevel)
                        )
                );
            }
        });
        
        if(gameScreen.getGame().getCurrentGameRoundController().getSelectedVacancy() != null){
            IMP03_Vacancy tmpVacancy = gameScreen.getGame().getCurrentGameRoundController().getSelectedVacancy();
            for(int i = 0; i < cbVacancy.getItemCount(); i++){
                if(tmpVacancy.getVacancyID() == ((IMP03_Vacancy)(cbVacancy.getItemAt(i))).getVacancyID()){
                    cbVacancy.setSelectedIndex(i);
                    cbVacancy.setEnabled(false);
                    break;
                }
            }
        } else if(cbVacancy.getItemCount() > 0){
            cbVacancy.setSelectedIndex(0);
        }
        
        layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .addGap(10)
                .addComponent(cbVacancy) //, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(20)
                .addComponent(vacancyParameters)//, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10)
        );

        layout.setVerticalGroup(
            layout.createSequentialGroup()
                .addGap(10)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(cbVacancy) //, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(vacancyParameters)//, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                )
                .addGap(10)
        );
    }
    
}
